//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Swaption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Instruments/VanillaSwap.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Option.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Instruments::CSwaption::CSwaption (Cephei::QL::Instruments::IVanillaSwap^ swap, Cephei::QL::IExercise^ exercise, Microsoft::FSharp::Core::FSharpOption<QL::Instruments::Settlement::TypeEnum>^ delivery, Cephei::QL::IPricingEngine^ QL_Pricer) : COption(CSwaption::typeid)
{
    CVanillaSwap^ _Cswap;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phSwaption = NULL;
#endif
        _Cswap = safe_cast<CVanillaSwap^> (swap);
        _Cswap->Lock();
        boost::shared_ptr<QuantLib::VanillaSwap>& _swap = static_cast<boost::shared_ptr<QuantLib::VanillaSwap>&> (_Cswap->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        QuantLib::Settlement::Type _delivery = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Instruments::Settlement::TypeEnum>::IsSome::get (delivery) ? (QuantLib::Settlement::Type)delivery->Value : QuantLib::Settlement::Type::Physical); //10
        _ppSwaption = new boost::shared_ptr<QuantLib::Swaption> (new QuantLib::Swaption ( _swap,  _exercise,  _delivery ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppSwaption)->setPricingEngine (_QL_Pricer);
        SetOption (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cswap != nullptr) _Cswap->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Instruments::CSwaption::CSwaption (boost::shared_ptr<QuantLib::Swaption>& childNative, Object^ owner) : COption(CSwaption::typeid)
{
#ifdef HANDLE
	_phSwaption = NULL;
#endif
	_ppSwaption = &childNative;
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
}
Cephei::QL::Instruments::CSwaption::CSwaption (QuantLib::Swaption& childNative, Object^ owner) : COption(CSwaption::typeid)
{
#ifdef HANDLE
	_phSwaption = NULL;
#endif
	_ppSwaption = new boost::shared_ptr<QuantLib::Swaption> (&childNative);
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
    _SwaptionOwner = owner;
    _OptionOwner = owner;
}

Cephei::QL::Instruments::CSwaption::CSwaption (CSwaption^ copy) : COption(CSwaption::typeid)
{
#ifdef HANDLE
	_phSwaption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwaption = new boost::shared_ptr<QuantLib::Swaption> (copy->GetShared());
        _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
    }
}
Cephei::QL::Instruments::CSwaption::CSwaption (PLATFORM::Type^ t) : COption(CSwaption::typeid)
{
#ifdef HANDLE
	_phSwaption = NULL;
#endif
	if (!t->IsSubclassOf(CSwaption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CSwaption::CSwaption (QuantLib::Handle<QuantLib::Swaption>& childNative, Object^ owner)  : COption(CSwaption::typeid)
{
	_phSwaption = &childNative;
	_ppSwaption = &static_cast<boost::shared_ptr<QuantLib::Swaption>>(childNative.currentLink());
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
    _SwaptionOwner = owner;
}
Cephei::QL::Instruments::CSwaption::CSwaption (QuantLib::Handle<QuantLib::Swaption> childNative)  : COption(CSwaption::typeid)
{
	_phSwaption = &childNative;
	_ppSwaption = &static_cast<boost::shared_ptr<QuantLib::Swaption>>(childNative.currentLink());
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CSwaption::CSwaption (QuantLib::Swaption childNative)  : COption(CSwaption::typeid)
{
#ifdef HANDLE
	_phSwaption = NULL;
#endif
	_ppSwaption = new boost::shared_ptr<QuantLib::Swaption> (new QuantLib::Swaption (childNative));
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppSwaption));
}
#endif

Cephei::QL::Instruments::CSwaption::~CSwaption ()
{
    if (_ppSwaption != NULL)
    {
	    delete _ppSwaption;
        _ppSwaption = NULL;
    }
}
Cephei::QL::Instruments::CSwaption::!CSwaption ()
{
    if (_ppSwaption != NULL)
    {
	    delete _ppSwaption;
    }
}
QuantLib::Swaption& Cephei::QL::Instruments::CSwaption::GetReference ()
{
    if (_ppSwaption == NULL) throw REFNEW NativeNullException ();
	return **_ppSwaption;
}
boost::shared_ptr<QuantLib::Swaption>& Cephei::QL::Instruments::CSwaption::GetShared ()
{
    if (_ppSwaption == NULL) throw REFNEW NativeNullException ();
	return *_ppSwaption;
}
QuantLib::Swaption* Cephei::QL::Instruments::CSwaption::GetPointer ()
{
    if (_ppSwaption == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwaption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Swaption>& Cephei::QL::Instruments::CSwaption::GetHandle ()
{
	if (_phSwaption == NULL)
	{
		_phSwaption = new Handle<QuantLib::Swaption> (*_ppSwaption);
	}
	return *_phSwaption;
}
#endif
bool Cephei::QL::Instruments::CSwaption::HasNative () 
{
	return (_ppSwaption != NULL);
}

Double Cephei::QL::Instruments::CSwaption::ImpliedVolatility (Double price, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Double guess, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt32>^ maxEvaluations, Microsoft::FSharp::Core::FSharpOption<Double>^ minVol, Microsoft::FSharp::Core::FSharpOption<Double>^ maxVol)
{
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
        QuantLib::Real _price = (QuantLib::Real)ValueHelper::Convert (price); //a
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        QuantLib::Volatility _guess = (QuantLib::Volatility)ValueHelper::Convert (guess); //a
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-4); //9a
        QuantLib::Natural _maxEvaluations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt32>::IsSome::get (maxEvaluations) ? (QuantLib::Natural)ValueHelper::Convert (maxEvaluations->Value) : 100); //9a
        QuantLib::Volatility _minVol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (minVol) ? (QuantLib::Volatility)ValueHelper::Convert (minVol->Value) : 1.0e-7); //9a
        QuantLib::Volatility _maxVol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (maxVol) ? (QuantLib::Volatility)ValueHelper::Convert (maxVol->Value) : 4.0); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppSwaption)->impliedVolatility ( _price,  _discountCurve,  _guess,  _accuracy,  _maxEvaluations,  _minVol,  _maxVol );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Boolean Cephei::QL::Instruments::CSwaption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppSwaption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Instruments::Settlement::TypeEnum Cephei::QL::Instruments::CSwaption::SettlementType::get ()
{
    try
    {
    	QuantLib::Settlement::Type _rv = (QuantLib::Settlement::Type)(*_ppSwaption)->settlementType ( );   
        QL::Instruments::Settlement::TypeEnum _nrv = (QL::Instruments::Settlement::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Instruments::VanillaSwap::TypeEnum Cephei::QL::Instruments::CSwaption::Type::get ()
{
    try
    {
    	QuantLib::VanillaSwap::Type _rv = (QuantLib::VanillaSwap::Type)(*_ppSwaption)->type ( );   
        QL::Instruments::VanillaSwap::TypeEnum _nrv = (QL::Instruments::VanillaSwap::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IVanillaSwap^ Cephei::QL::Instruments::CSwaption::UnderlyingSwap::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::VanillaSwap>& _rv = (boost::shared_ptr<QuantLib::VanillaSwap>&)(*_ppSwaption)->underlyingSwap ( );   
        Cephei::QL::Instruments::CVanillaSwap^ _nrv = REFNEW Cephei::QL::Instruments::CVanillaSwap (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ISwaption^ Cephei::QL::Instruments::CSwaption_Factory::Create (Cephei::QL::Instruments::IVanillaSwap^ swap, Cephei::QL::IExercise^ exercise, Microsoft::FSharp::Core::FSharpOption<QL::Instruments::Settlement::TypeEnum>^ delivery, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CSwaption ( swap,  exercise,  delivery,  QL_Pricer);
}
